package com.igeek.boot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeek.boot.entity.User;
import com.igeek.boot.entity.UserExample;
import com.igeek.boot.mapper.UserMapper;
import com.igeek.boot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * *User 訫
 * *Description TODO
 * *Date 2024/8/20
 **/
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    public int addUser(User user){
        return userMapper.insert(user);
    }

    public int updateUser(Integer id,String pic){
        User user = new User(id,pic);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public int deleteUser(Integer id){
        return userMapper.deleteByPrimaryKey(id);
    }

    public List<User> getUser(String name) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andNameLike("%"+name+"%");
        List<User> users = userMapper.selectByExample(userExample);
        return users;
    }

    @Override
    public PageInfo<User> getUserByPage(String name) {
        //开启分页
        PageHelper.startPage(1,2);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andNameLike("%"+name+"%");
        List<User> users = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }
}
